package com.leetcode.array;

import java.util.ArrayList;
import java.util.List;
/**
 * Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]
 * @author Adiy
 *
 */
public class SummaryRanges {

	public static void main(String[] args) {
		int nums[]={};
		List<String> result=summaryRanges(nums);
		System.out.println(result);

	}

    public static List<String> summaryRanges(int[] nums) {
        int i=0,len=nums.length;
        List<String> list=new ArrayList<String>();
        if(len==0) return list;
        int begin=nums[0];
             
        for(i=0;i<len;i++){
        	if(i+1<len&&nums[i]+1!=nums[i+1]){
        		if(begin==nums[i]){
        			list.add(""+begin+"");
        		}else{
        			list.add(begin+"->"+nums[i]);	
        		}
        		begin=nums[i+1];
        	}
             if(i+1==len&&nums[i]+1!=nums[len-1]){
        		if(begin==nums[i]){
        			list.add(""+begin+"");
        		}else{
        			list.add(begin+"->"+nums[i]);	
        		}
        		begin=nums[len-1];
        	}
        }
        return list;
    }
}
